package io.modifier.basic.utils.network

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import io.modifier.basic.core.appCtx.AppCtx
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.mapLatest

@SuppressLint("StaticFieldLeak")
object FNetwork {
    /** 当前网络 */
    val currentNetwork: NetworkState
        get() = _networksConnectivity.currentNetwork

    /** 监听当前网络 */
    val currentNetworkFlow: Flow<NetworkState>
        get() = _networksConnectivity.currentNetworkFlow

    /** 监听所有网络 */
    val allNetworksFlow: Flow<List<NetworkState>>
        get() = _networksConnectivity.allNetworksFlow

    val netEnableFlow: Flow<Boolean>
        get() = _networksConnectivity.currentNetworkFlow.mapLatest { it.isConnected }

    private val _networksConnectivity by lazy {
        val context = AppCtx.context
        val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        NetworksConnectivity(manager)
    }

}

/**
 * 如果当前网络不满足[condition]，则挂起直到满足[condition]，默认[condition]为网络已连接
 * @return true-调用时已经满足[condition]；false-调用时还不满足[condition]，挂起等待之后满足[condition]
 */
suspend fun fAwaitNetwork(
    condition: (NetworkState) -> Boolean = { it.isConnected },
): Boolean {
    if (condition(FNetwork.currentNetwork)) return true
    FNetwork.currentNetworkFlow.first { condition(it) }
    return false
}
